package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

import com.Global.Global;
import com.code.chart.AbstractDemoChart;

public class Chart_DoanhThuThang  extends AbstractDemoChart{


	private double iMaxY = 0;
	private int iMonthNowPlus = Calendar.getInstance().get(Calendar.MONTH) + 2;
	private int iMonthNow = Calendar.getInstance().get(Calendar.MONTH) + 1;
	private int iYearNowAbt = Calendar.getInstance().get(Calendar.YEAR) - 1;
	private int iYearNow = Calendar.getInstance().get(Calendar.YEAR);
	

	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	public Intent execute(Context context,String kho) {
		double dbAllDoanhThu[] = getDoanhThuByMoth(kho);
		// double dbCallHasSPInYear[] =
		// getCallInYear(" AND SoPhieu IS NOT NULL");
		String[] titles = new String[] { "Doanh thu theo tháng (VNĐ/tháng)" };
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++) {			
			for (int j = 0; j <= 12; j++) {
				x.add(new double[] { j + 1 });
			}		
		}
		List<double[]> values = new ArrayList<double[]>();
		values.add(dbAllDoanhThu);
		// values.add(dbCallHasSPInYear);
		int[] colors = new int[] { Color.GREEN };
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		// iMaxY = iMaxY * 12 / 10;
		setChartSettings(renderer, "", "Tháng", "", 0.5, 12.5, 0, iMaxY,
				Color.GRAY, Color.LTGRAY);
		renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		// renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
		renderer.setXLabels(0);
		renderer.setYLabels(10);

		for (int i = iMonthNow; i < iMonthNow + 13; i++) {
			renderer.addXTextLabel(i - iMonthNow + 1, returnstrMonth(i));
		}
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.CENTER);
		renderer.setPanEnabled(true, false);
		// renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);
		renderer.setZoomButtonsVisible(true);
		return ChartFactory.getBarChartIntent(context,
				buildBarDataset(titles, values), renderer, Type.STACKED,
				"TK doanh thu từ " + String.valueOf(iMonthNowPlus) + "/"
						+ String.valueOf(iYearNowAbt) + " đến "
						+ String.valueOf(iMonthNow) + "/"
						+ String.valueOf(iYearNow) );
	}

	/**
	 * @author trangbm
	 * @return date 24/04/2014
	 */
	private double[] getDoanhThuByMoth(String kho) {
		double dbThangSLGoi[] = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0 };
		StringBuffer sql =new StringBuffer("SELECT MONTH(Ngay) Thang,  DATEPART(YEAR,Ngay) Nam ,SUM(Thanh_Tien) Tien");
				sql.append(" FROM PhieuXuat JOIN VatTuXuat ON PhieuXuat.SoCt = VatTuXuat.SoCt");
				sql.append( " WHERE Ngay > = '" + String.valueOf(iYearNowAbt) + "/"+ String.valueOf(iMonthNow) + "/1'"); 
				if(!kho.equals(""))
					sql.append(" AND  MSK COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"+kho+"', ','))");
				sql.append(" GROUP BY MONTH(Ngay), DATEPART(YEAR,Ngay) " + " ORDER BY DATEPART(YEAR,Ngay), MONTH(Ngay)");
		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
		try {
			while (rs.next()) {
				int iMonthIndex = Integer.parseInt(rs.getString("Thang")
						.toString());
				double iTienIndex =  Global.convertMooneyByDouble(Double.parseDouble(rs.getString("Tien")
						.toString()));
				dbThangSLGoi[returniMonth(iMonthIndex - iMonthNow + 12) ] = (double) iTienIndex;

				dbThangSLGoi[0] = 0;
				dbThangSLGoi[12]= 0;
				if(rs.getString("Thang").toString().equals(String.valueOf(iMonthNow)) && rs.getString("Nam").toString().equals(String.valueOf(iYearNowAbt)))
					dbThangSLGoi[0] =  Global.convertMooneyByDouble(Double.parseDouble(rs.getString("Tien")
							.toString()));
				if(rs.getString("Thang").toString().equals(String.valueOf(iMonthNow)) && rs.getString("Nam").toString().equals(String.valueOf(iYearNow)))
					dbThangSLGoi[12] =  Global.convertMooneyByDouble(Double.parseDouble(rs.getString("Tien").toString()));
				// set max Y
				if (iTienIndex > iMaxY)
					iMaxY = iTienIndex;
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dbThangSLGoi;
	}

	public String returnstrMonth(int iMonth) {
		if (iMonth == 12 || iMonth == 0)
			return String.valueOf(12);
		else
			return String.valueOf(iMonth % 12);
	}

	public int returniMonth(int iMonth) {
		if (iMonth == 12 || iMonth == 0)
			return 12;
		else
			return iMonth % 12;
	}

	public Intent execute(Context context) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
